package org.fullhappy.ajax.load;
import java.io.IOException;
import java.io.PrintWriter;
import java.lang.reflect.Method;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class LoadResources implements Filter {

    @Override
    public void init(FilterConfig config) throws ServletException {
        //
    }

    @Override
    public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws ServletException, IOException {
        HttpServletRequest request = (HttpServletRequest) req;
        HttpServletResponse response = (HttpServletResponse) res;
        String requestURI = request.getRequestURI();
        String[] paths = requestURI.substring(1).split("/");
        String[] paths1 = new String[paths.length-1];
        System.arraycopy(paths, 1, paths1, 0, paths1.length);
        if(paths.length >= 2){
        	Method method = null;
        	try {
        		method = this.getClass().getDeclaredMethod(paths[1], HttpServletRequest.class, HttpServletResponse.class);
        		if(method != null){
        			method.setAccessible(true);
        			method.invoke(this, request,response);
        		}
        		
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
        }else{
        	response.setStatus(HttpServletResponse.SC_NOT_FOUND);
        }
        
    }

    @Override
    public void destroy() {
        //
    }
    
    @SuppressWarnings("unused")
	private void menu(HttpServletRequest request, HttpServletResponse response) throws IOException{
    	response.setContentType("application/json");
    	response.setCharacterEncoding("UTF-8");
        PrintWriter out = response.getWriter();
        
        String s= ""+
        		"		[{'title':'hone','link':'/'},                                                                                                                                                                                                                                                                                       "+
        	    "            {'title':'product','link':'#','child':                                                                                                                                                                                                                                                                                   "+
        	    "            	[                                                                                                                                                                                                                                                                                                                    "+
        	    "            	 {'title':'Product1','link':'#','child':[{'title':'Product11','link':'/show_room/2'},{'title':'Product12','link':'/show_room/2'},{'title':'Product13','link':'/show_room/2'},{'title':'Product14','link':'/show_room/2'},{'title':'+ Add New Item','link':'javascript:(new Menu()).openLightBoxAddGalleryItem();'}]},                                 "+
        	    "            	 {'title':'Product2','link':'#','child':[{'title':'Product21','link':'/show_room/2'},{'title':'Product22','link':'/show_room/2'},{'title':'+ Add New Item','link':'javascript:(new Menu()).openLightBoxAddGalleryItem();'}]},                                                                                                                                 "+
        	    "            	 {'title':'Product3','link':'#','child':[{'title':'Product31','link':'/show_room/2'},{'title':'Product32','link':'/show_room/2'},{'title':'Product33','link':'/show_room/2'},{'title':'Product34','link':'/show_room/2'},{'title':'Product35','link':'/show_room/2'},{'title':'+ Add New Item','link':'javascript:(new Menu()).openLightBoxAddGalleryItem();'}]},   "+
        	    "            	 {'title':'Product4','link':'#','child':[{'title':'Product41','link':'/show_room/2'},{'title':'+ Add New Item','link':'javascript:(new Menu()).openLightBoxAddGalleryItem();'}]},                                                                                                                                                                     "+
        	    "            	 {'title':'Product5','link':'#','child':[{'title':'Product51','link':'/show_room/2'},{'title':'Product52','link':'/show_room/2'},{'title':'+ Add New Item','link':'javascript:(new Menu()).openLightBoxAddGalleryItem();'}]},                                                                                                                                                                                   "+
        	    "            	 {'title':'Product6','link':'#','child':[{'title':'Product61','link':'/show_room/2'},{'title':'Product62','link':'/show_room/2'},{'title':'+ Add New Item','link':'javascript:(new Menu()).openLightBoxAddGalleryItem();'}]},                                                                                                                                     "+
        	    "            	 {'title':'Add New ','link':'javascript:(new Menu()).openLightBoxAddGallery();','child':[{'title':'--','link':'#'},{'title':'--','link':'#'}]}                                                                                                                      "+
        	    "            	]                                                                                                                                                                                                                                                                                                                    "+
        	    "			},                                                                                                                                                                                                                                                                                                                       "+
        	    "            {'title':'Sale','link':'/sale.html'},                                                                                                                                                                                                                                                                                    "+
        	    "            {'title':'About Us','link':'/about_us.html'},                                                                                                                                                                                                                                                                                "+
        	    "            {'title':'Contact Us','link':'#','child':[{'title':'Address','link':'#'},{'title':'Send Email','link':'#'}]}]                                                                                                                                                                                                              "+
        		"";
        out.println(s);
        out.flush();
        out.close();
    }
    
    @SuppressWarnings("unused")
	private void product(HttpServletRequest request, HttpServletResponse response) throws IOException{    	
        
        String requestURI = request.getRequestURI();
        String[] paths = requestURI.substring(1).split("/");
        //   product/123
        if(paths.length>=3){
        	String id = paths[2];
        	String s= getProductResource(id);        	
        	response.setContentType("application/json");
        	response.setCharacterEncoding("UTF-8");
        	
            PrintWriter out = response.getWriter();
            out.println(s);
            out.flush();
            out.close();
        }else {
        	response.setStatus(HttpServletResponse.SC_NOT_FOUND);
		}
        
    }

	private String getProductResource(String id) {
		return 	"{"+
		        		"	'title' : 'Đầm ren tay con duyên dáng MS 122',											"+				
						"	'description' : '- Đầm được may bằng chất liệu phi bóng nhập khẩu, mềm mại, thoáng "+
						"					mát, mịn màng. <br> - Kiểu dáng thanh lịch, kết hợp kiểu cánh     "+
						"					dơi với đầm tôn vẻ sang trọng của bạn gái. <br> - Màu sắc:        "+
						"					nhiều màu. <br> - Đầm free size, thích hợp cho các bạn gái từ     "+
						"					55kg trở xuống. <br> - Đầm phi sẽ giúp bạn dịu dàng khi dạo       "+
						"					phố, thanh lịch khi đi làm, sang trọng khi đi dự tiệc. <br> -     "+
						"					Có thể mua để làm quà tặng ý nghĩa dành cho các bạn gái	',			"+		
						"	'price':'499.000đ',																		"+
						"	'images':['http://www.vatgia.com/pictures_fullsize/lku1300533839.jpg',					"+					
						"	          'http://www.vatgia.com/pictures_fullsize/dyq1301283412.jpg',					"+					
						"	          'http://www.vatgia.com/pictures_fullsize/xjq1301931051.jpg',					"+					
						"	          'http://www.vatgia.com/pictures_fullsize/iaw1304662568.jpg',					"+					
						"	          'http://www.vatgia.com/pictures_fullsize/fqu1304663118.jpg',					"+					
						"	          'http://www.vatgia.com/pictures_fullsize/fqu1304663118.jpg',					"+
						"	          'http://www.vatgia.com/pictures_fullsize/fqu1304663118.jpg']					"+					
						"}";
	}
 
 }

